<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 日期限制控制器示例
 * 展示如何在FastAdmin中使用自定义日期选择器
 */
class DateRestriction extends Backend
{
    protected $model = null;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加页面
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                // 处理表单提交
                $this->success('添加成功');
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        // 定义不同的日期限制配置
        $dateConfigs = [
            // 仅工作日配置
            'workdays_only' => [
                'restrictionType' => 'workdays',
                'description' => '仅允许选择工作日（周一到周五）'
            ],
            
            // 仅周末配置
            'weekends_only' => [
                'restrictionType' => 'weekends',
                'description' => '仅允许选择周末（周六、周日）'
            ],
            
            // 自定义工作日配置（周一、周三、周五）
            'custom_weekdays' => [
                'restrictionType' => 'custom',
                'allowedWeekdays' => [1, 3, 5], // 周一、周三、周五
                'description' => '仅允许选择周一、周三、周五'
            ],
            
            // 指定日期配置
            'specific_dates' => [
                'restrictionType' => 'specific',
                'allowedDates' => [
                    date('Y-m-d', strtotime('+1 day')),
                    date('Y-m-d', strtotime('+3 days')),
                    date('Y-m-d', strtotime('+7 days')),
                    date('Y-m-d', strtotime('+14 days'))
                ],
                'description' => '仅允许选择指定的几个日期'
            ],
            
            // 禁用特定日期配置
            'disabled_dates' => [
                'restrictionType' => 'disabled',
                'disabledDates' => [
                    date('Y-m-d', strtotime('+2 days')),
                    date('Y-m-d', strtotime('+5 days'))
                ],
                'description' => '禁用特定日期，其他日期可选'
            ],
            
            // 动态配置示例（从数据库读取）
            'dynamic_config' => $this->getDynamicDateConfig()
        ];

        // 将配置传递给视图
        $this->assign('dateConfigs', json_encode($dateConfigs, JSON_UNESCAPED_UNICODE));
        return $this->view->fetch();
    }

    /**
     * 编辑页面
     */
    public function edit($ids = null)
    {
        // 模拟从数据库读取的记录
        $row = [
            'id' => $ids,
            'title' => '测试课程',
            'course_date' => date('Y-m-d'),
            'restriction_type' => 'workdays_only'
        ];

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                // 处理表单提交
                $this->success('编辑成功');
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        // 根据记录的限制类型获取相应配置
        $dateConfig = $this->getDateConfigByType($row['restriction_type']);
        
        $this->assign('row', $row);
        $this->assign('dateConfig', json_encode($dateConfig, JSON_UNESCAPED_UNICODE));
        return $this->view->fetch();
    }

    /**
     * 获取动态日期配置（从数据库或其他数据源）
     */
    private function getDynamicDateConfig()
    {
        // 这里可以从数据库、配置文件或其他数据源读取
        // 示例：从数据库读取课程安排，只允许选择有课程的日期
        
        // 模拟从数据库查询可用日期
        $availableDates = [
            date('Y-m-d', strtotime('+1 day')),
            date('Y-m-d', strtotime('+3 days')),
            date('Y-m-d', strtotime('+5 days')),
            date('Y-m-d', strtotime('+7 days')),
            date('Y-m-d', strtotime('+10 days')),
            date('Y-m-d', strtotime('+14 days'))
        ];

        return [
            'restrictionType' => 'specific',
            'allowedDates' => $availableDates,
            'description' => '仅允许选择有课程安排的日期'
        ];
    }

    /**
     * 根据类型获取日期配置
     */
    private function getDateConfigByType($type)
    {
        $configs = [
            'workdays_only' => [
                'restrictionType' => 'workdays'
            ],
            'weekends_only' => [
                'restrictionType' => 'weekends'
            ],
            'custom_weekdays' => [
                'restrictionType' => 'custom',
                'allowedWeekdays' => [1, 3, 5]
            ],
            'specific_dates' => [
                'restrictionType' => 'specific',
                'allowedDates' => [
                    date('Y-m-d', strtotime('+1 day')),
                    date('Y-m-d', strtotime('+3 days')),
                    date('Y-m-d', strtotime('+7 days'))
                ]
            ],
            'disabled_dates' => [
                'restrictionType' => 'disabled',
                'disabledDates' => [
                    date('Y-m-d', strtotime('+2 days')),
                    date('Y-m-d', strtotime('+5 days'))
                ]
            ]
        ];

        return isset($configs[$type]) ? $configs[$type] : [];
    }

    /**
     * API接口：获取日期配置
     */
    public function getDateConfig()
    {
        $type = $this->request->get('type', 'workdays_only');
        $config = $this->getDateConfigByType($type);
        
        $this->success('获取成功', null, $config);
    }
}